<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_jpc extends CI_Model {

	public function __construct() 
	{
		parent::__construct();
		
		$this->load->helper(array('form','captcha','array','download'));
		$this->load->library(array('form_validation','session','table','sam','tank_auth','upload'));
		$this->load->database();
		//$this->model_auth->_must_logged_in('jpc');
	}
	
	public function _get_all () {
		$this->db->order_by("jpc_no_tim", "asc");
		$query = $this->db->get('jpc');
		$data = $query->result();
		return $data;	
	}	
	
	public function _get_valid () {
		$this->db->order_by("jpc_no_tim", "asc");
		$query = $this->db->get_where('jpc',array('jpc_valid' => 1));
		$data =$query->result();
		return $data;	
	}

	public function _get_not_valid () {
		$this->db->order_by("jpc_no_tim", "asc");
		$query = $this->db->get_where('jpc',array('jpc_valid' => 0));
		$data =$query->result();
		return $data;	
	}
	
	public function _get_finalis () {
		$this->db->order_by("jpc_no_tim", "asc");
		$query = $this->db->get_where('jpc',array('jpc_finalis' => 1));
		$data = $query->result();
		return $data;	
	}
	
	public function _get_userdata ($num='default') {
		$query = $this->db->get_where('jpc',array('jpc_no_tim' => (($num=='default')?$this->session->userdata('usertype_id'):$num)));
		$data = $query->row();
		return $data;	
	}
	
	public function _edit_kontestan ($data, $usertype_id='') {
		if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
			$select= array(	
				'jpc_nama',
				'jpc_nohp',
				'jpc_email',
				'jpc_kelas',
				'jpc_nama_sek',
				'jpc_alamat_sek',
				'jpc_pembimbing',
			);
		$data= elements($select, $data);

		$this->db->where('jpc_no_tim', $usertype_id);
		$this->db->update('jpc', $data); 		

	}
	
	public function _insert_scan_data ($data, $usertype_id='') {
		//if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
		$userdata = $this->_get_userdata();
	
		//jpc_link_ktm1 	jpc_link_ktm2 	jpc_link_ktm3 	jpc_link_buktibayar
		$update = array(
			'jpc_link_kpelajar'			=>
				($data['jpc_link_kpelajar']!='')?$data['jpc_link_kpelajar']:$userdata->jpc_link_kpelajar,	
			'jpc_link_buktibayar'	=> 
				($data['jpc_link_buktibayar']!='')?$data['jpc_link_buktibayar']:$userdata->jpc_link_buktibayar,		
		);
			
		//print_r($retval);
		$this->db->where('jpc_no_tim', $userdata->jpc_no_tim);
		$this->db->update('jpc', $update); 		
		
		//return $retval;
	}
	
	public function _insert_kaos_data ($data, $usertype_id='') {
		//if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
		$userdata = $this->_get_userdata();
	
		//jpc_link_ktm1 	jpc_link_ktm2 	jpc_link_ktm3 	jpc_link_buktibayar
		$select = array ('jpc_kaos');
		$data= elements($select, $data);
		$this->db->where('jpc_no_tim', $userdata->jpc_no_tim);
		$this->db->update('jpc', $data); 		
		
		//return $retval;
	}
	
	public function _create_peserta_junior ($data = NULL) {	
		/*
		register	
			dx_auth/user -> function set_user($user_id, $data)
		*/
	  
		$select= array(	'jpc_nama',
						'jpc_nohp',
						'jpc_email',
						'jpc_kelas',
						'jpc_nama_sek',
						'jpc_alamat_sek',
						'jpc_pembimbing',
						'jpc_agreement'
						);
		//ambil data dengan index select dari post
		$data= elements($select, $data);
		//print_r($data);
		$query = $this->db->get_where('jpc', $data);
		//print_r($query);
		
		if ($query->num_rows()==0) {
		// if (1) {
		
			$sql  = $this->db->insert_string('jpc',$data);
			$query= $this->db->query($sql);
			
			//tarik dari database cari no reg nya
			$query = $this->db->get_where('jpc', $data);
			$no_reg  = $query->row('jpc_no_tim');
			$email  = $query->row('jpc_email');
			
			//atur username and passwordnya
			$username = sprintf("JPC%03d",$no_reg);
			$password = $this->sam->_random_string(10);
			// $password_encrypted = sha1($password);
			
			$usertype='jpc';
			$usertype_id=$no_reg;
			//print_r($data);	
			
			$this->tank_auth->create_user($username, $email, $password,$usertype,$usertype_id, FALSE);
		
			$data2= array (	'username' => $username,
							'def_password' => $password,
							'email' => $email);
			
			$mailparam = (object) $data;
			$mailparam->username=$username;
			$mailparam->password=$password;
			
			$this->_sendmail($mailparam);

			$this->session->set_userdata($data2);

			// $data= array (	'username' => $username,
							// 'def_password' => $password,
							// 'email' => $email);
			
			// $this->_sendmail($data);
			
			// $this->session->set_userdata($data);
			return 1;
		}  else
			return 0;
	}
	
	
	public function _sendmail($data) {
		$this->load->library('email');
		$config['protocol'] = 'sendmail';
		$config['mailtype'] = 'html';
		$this->email->initialize($config);
	
		// print_r($data);
		// $userdata=$data['userdata'];
////////////////////////////////////////////////////		
///////////////      KONTEN EMAIL 
		$email = $data->jpc_email;
		$subject = 'Pendaftaran ITB Junior Programming Contest 2011';
								
		// $email_debug=TRUE;
		$email_debug=FALSE;
		$content=
			// ''
			$this->load->view('email/jpc_pendaftaran',array('userdata'=>$data),!$email_debug)
			;
		
			// ';
		// $altcontent='';
		
////////////////////////////////////////////////////
////////////////////////////////////////////////////
	
		
		$this->email->from('itbpc@if.itb.ac.id', 'ITB Programming Contest 2011 Commitee');
		$this->email->to($email);
		$this->email->bcc('itbpc.official@gmail.com');
		$this->email->reply_to('itbpc.official@gmail.com', 'ITB Programming Contest 2011 Commitee');

		$this->email->subject($subject);
		$this->email->message($content);
		// $this->email->set_alt_message($altcontent);
		
		$this->email->send();
		if ($email_debug) {echo $this->email->print_debugger();}
	
		
	}
	
	public function _make_warning ($num='default') {
		
		$data= $this->_get_userdata (($num=='default')?$this->session->userdata('usertype_id'):$num);	
		$retval = array (
			'exist' => (($data->jpc_link_kpelajar=='')
						||$data->jpc_link_buktibayar==''
						||($data->jpc_finalis==1&&$data->jpc_kaos==''))?1:0,
			'scankpelajar'=>($data->jpc_link_kpelajar=='')?1:0,
			'buktibayar'=>($data->jpc_link_buktibayar=='')?1:0,
			'kaos'		=>($data->jpc_finalis==1&&
							$data->jpc_kaos=='')?1:0,
			);
		return $retval;
	}

}